package com.ruoyi.mushcoin.mapper;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.mushcoin.domain.TgUserInfo;
import com.ruoyi.mushcoin.domain.vo.TgUserInfoVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * TG用户Mapper接口
 *
 * @author billy
 * 2024-07-29
 */
@Mapper
public interface TgUserInfoMapper extends BaseMapper<TgUserInfo> {
    @Select("""
                select id,user_id,first_name,last_name,user_name,is_premium,mushcoin,tui.invite_user_id,create_time,update_time ,invite_user_count
        ,ifnull(iu.gold,0) gold,ifnull(iu.diamond,0) diamond,ifnull(iu.mush,0) mush,ifnull(iu.totalReward,0) invite_rewards_total
        from tg_user_info tui
        join (
        select invite_user_id, sum(game_data->'$."user-currency"."2"') gold,sum(game_data->'$."user-currency"."201"') diamond,sum(mushcoin) mush ,sum(invite_rewards_total) totalReward
        from tg_user_info where invite_user_id is not null group by invite_user_id
        )iu on iu.invite_user_id=tui.user_id
        """)
    List<TgUserInfoVo> inviteUserRewardsPage(QueryWrapper queryWrapper);

    @Select("""
                select id,user_id,first_name,last_name,user_name,is_premium,mushcoin,tui.invite_user_id,create_time,update_time ,invite_user_count
        ,ifnull(iu.gold,0) gold,ifnull(iu.diamond,0) diamond,ifnull(iu.mush,0) mush,ifnull(iu.totalReward,0) invite_rewards_total
        from tg_user_info tui
        join (
        select invite_user_id, sum(game_data->'$."user-currency"."2"') gold,sum(game_data->'$."user-currency"."201"') diamond,sum(mushcoin) mush ,sum(invite_rewards_total) totalReward
        from tg_user_info where invite_user_id is not null group by invite_user_id
        )iu on iu.invite_user_id=tui.user_id
        """)
    Page<TgUserInfoVo> inviteUserRewardsPageTest(@Param("page") Page<TgUserInfoVo> page);

}
